package com.ruoyi.web.ydz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.NewsDisease;
import com.ruoyi.web.ydz.service.INewsDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.ydz.mapper.NewsMapper;
import com.ruoyi.web.ydz.domain.News;
import com.ruoyi.web.ydz.service.INewsService;

/**
 * 新闻Service业务层处理
 * 
 * @author DRX
 * @date 2024-10-31
 */
@Service
public class NewsServiceImpl implements INewsService 
{
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private INewsDiseaseService newsDiseaseService;

    /**
     * 查询新闻
     * 
     * @param id 新闻主键
     * @return 新闻
     *
     */
    @Override
    public News selectNewsById(Long id)
    {
        News news = newsMapper.selectNewsById(id);
        if (Objects.isNull(news)){
            return null;
        }
        NewsDisease newsDisease = new NewsDisease();
        newsDisease.setNewsId(id);
        List<NewsDisease> list = newsDiseaseService.selectNewsDiseaseList(newsDisease);
        news.setNewsDiseaseList(list);
        return news;
    }

    /**
     * 查询新闻列表
     * 
     * @param news 新闻
     * @return 新闻
     */
    @Override
    public List<News> selectNewsList(News news)
    {
        return newsMapper.selectNewsList(news);
    }

    /**
     * 新增新闻
     * 
     * @param news 新闻
     * @return 结果
     */
    @Override
    public int insertNews(News news)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        news.setCreateTime(DateUtils.getNowDate());
        news.setCreateBy(user.getUserName());
        news.setCreateName(user.getNickName());

        news.setUpdateTime(DateUtils.getNowDate());
        news.setUpdateBy(user.getUserName());
        news.setUpdateName(user.getNickName());
        if(news.getNewsDiseaseList().size()>0) {
            news.getNewsDiseaseList().forEach(item->{
                item.setNewsId(news.getId());
                newsDiseaseService.insertNewsDisease(item);
            });
        }
        return newsMapper.insertNews(news);
    }


    @Override
    public int updateNews(News news)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        news.setUpdateBy(user.getNickName());
        news.setUpdateName(user.getUserName());
        news.setUpdateTime(DateUtils.getNowDate());
        //删除疾病关联信息
        if(news.getDelNewsDiseaseIds().length>0){
            newsDiseaseService.deleteNewsDiseaseByIds(news.getDelNewsDiseaseIds());
        }
        if(null != news.getNewsDiseaseList() && news.getNewsDiseaseList().size()>0) {
            news.getNewsDiseaseList().forEach(item->{
                if(item.getId()!= null){
                    newsDiseaseService.updateNewsDisease(item);
                }else{
                    item.setNewsId(news.getId());
                    newsDiseaseService.insertNewsDisease(item);
                }
            });
        }

        return newsMapper.updateNews(news);
    }

    /**
     * 批量删除新闻
     * 
     * @param ids 需要删除的新闻主键
     * @return 结果
     */
    @Override
    public int deleteNewsByIds(Long[] ids)
    {
        return newsMapper.deleteNewsByIds(ids);
    }


    /**
     * 删除新闻信息
     * 
     * @param id 新闻主键
     * @return 结果
     */
    @Override
    public int deleteNewsById(Long id)
    {
        return newsMapper.deleteNewsById(id);
    }

    /**
     * 查询APP首页推荐 3条
     * 视图：app_index_recommend
     * @return 新闻集合
     */
    public List<News> selectIndexRecommend(){
        return newsMapper.selectIndexRecommend();
    }

    @Override
    public AjaxResult getUpAndDown(Long id){

        AjaxResult ajaxResult = new AjaxResult();
        try{
            
            List<HashMap<String, Object>> upandDown = newsMapper.getUpandDown(id);
            ajaxResult.put("data",upandDown);
            ajaxResult.put("code",200);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",500);
            ajaxResult.put("msg","获取新闻上下篇失败，请联系管理员！");
        }
        return ajaxResult;
    }



    /**
     * 增加浏览量
     *
     * @param id
     * @return 结果
     */
    public void updateNewsHis(Long id){
        newsMapper.updateNewsHis(id);
    }

    /**
     * 查询新闻列表
     *
     * @param news 新闻
     * @return 新闻集合
     */
    public List<News> getNewsList(News news){
        return newsMapper.getNewsList(news);
    }

    public AjaxResult selectNewsByLabel(News news){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            List<News> upandDown = newsMapper.selectNewsByLabel(news);
            ajaxResult.put("data",upandDown);
            ajaxResult.put("code",200);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code",500);
            ajaxResult.put("msg","获取新闻失败，请联系管理员！");
        }
        return ajaxResult;
    }
}
